//
//  SubCollectionCellThree.m
//  喜马拉雅FM
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT self.bounds.size.height

#import "SubCollectionCellThree.h"
#import "ModelForCollectionViewTwo.h"
#import "UIImageView+WebCache.h"


#import "NetworkHandlerBlock.h"
#import "DetailOfBookViewController.h"
#import "ModelForTypeOfDetail.h"
#import "ModelForTypeOfListOfDetail.h"

#import "ModelForListen.h"
@interface SubCollectionCellThree ()

@property (nonatomic, retain) UIView *view;

@property (nonatomic, retain) NSMutableArray *arrTempCoverPath;
@property (nonatomic, retain) NSMutableArray *arrTempTitle;


@property (nonatomic, retain) NSMutableArray *arrForJiaoYuPeiXun;
@property (nonatomic, retain) NSMutableArray *arrForMingXiaoGongKaiKe;
@property (nonatomic, retain) NSMutableArray *arrForBaiJiaJiangTan;
@property (nonatomic, retain) NSMutableArray *arrForGuangBoJu;
@property (nonatomic, retain) NSMutableArray *arrForXiQu;
@property (nonatomic, retain) NSMutableArray *arrForDianTai;

@end

@implementation SubCollectionCellThree

- (void)dealloc {

    [_view release];
    [_arrTempCoverPath release];
    [_arrTempTitle release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.arrForJiaoYuPeiXun = [NSMutableArray array];
        self.arrForMingXiaoGongKaiKe = [NSMutableArray array];
        self.arrForBaiJiaJiangTan = [NSMutableArray array];
        self.arrForGuangBoJu = [NSMutableArray array];
        self.arrForXiQu = [NSMutableArray array];
        self.arrForDianTai = [NSMutableArray array];
        
        
        [self createSubViews];
        [self handleData];
        
        
    }
    return self;
}

- (void)handleData {
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=13&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ///////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForJiaoYuPeiXun addObject:modelOfBook];
            }
        }
    }];
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=27&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ///////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForMingXiaoGongKaiKe addObject:modelOfBook];
            }
        }
    }];

    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=14&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    /////////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForBaiJiaJiangTan addObject:modelOfBook];
            }
        }
    }];
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=15&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    //////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForGuangBoJu addObject:modelOfBook];
            }
        }
    }];
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=16&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    /////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForXiQu addObject:modelOfBook];
            }
        }
    }];
    
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=17&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    //////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForDianTai addObject:modelOfBook];
            }
        }
    }];
    
}

- (void)createSubViews {
    
    self.view = [[UIView alloc]initWithFrame:self.bounds];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.view];
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            UIImageView *picture = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
            picture.frame = CGRectMake(WIDTH / 2 * j + 10,HEIGHT / 3  * i + 10,HEIGHT / 3 - 20,HEIGHT / 3 - 20);
            [self.view addSubview:picture];
            picture.tag = 10*i + j + 1;
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(WIDTH / 2 * j, HEIGHT / 3 * i, WIDTH / 2 - 2, HEIGHT / 3 - 2);
            [button setTitleEdgeInsets:UIEdgeInsetsMake(10, HEIGHT / 3 , 10, 10)];
            [button setTitle:@"3D体验馆" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [self.view addSubview:button];
            button.tag = 100*i + j + 15;
        }
    }
}

- (void)passModelForCellThree:(NSArray *)arrForModel {
    if (arrForModel) {
        
    
    self.arrTempCoverPath = [NSMutableArray array];
    self.arrTempTitle = [NSMutableArray array];
    for (ModelForCollectionViewTwo *model in arrForModel) {
        [self.arrTempCoverPath addObject:model.coverPath];
        [self.arrTempTitle addObject:model.title];
    }
    
    // 此处赋值时注意button和picture的tag标记的重复.
    UIImageView *pic = [self.view viewWithTag:1];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[11]]];
    
    pic = [self.view viewWithTag:2];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[12]]];
    
    
    pic = [self.view viewWithTag:11];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[13]]];
    
    pic = [self.view viewWithTag:12];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[14]]];
    
    pic = [self.view viewWithTag:21];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[15]]];
    
    pic = [self.view viewWithTag:22];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[16]]];
    
    UIButton *button = [self.view viewWithTag:15];
    [button setTitle:self.arrTempTitle[11] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:16];
    [button setTitle:self.arrTempTitle[12] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(enterToDetailOfBookViewController1:) forControlEvents:UIControlEventTouchUpInside];

    
    button = [self.view viewWithTag:115];
    [button setTitle:self.arrTempTitle[13] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController2:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:116];
    [button setTitle:self.arrTempTitle[14] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController3:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:215];
    [button setTitle:self.arrTempTitle[15] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController4:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:216];
    [button setTitle:self.arrTempTitle[16] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController5:) forControlEvents:UIControlEventTouchUpInside];
    
}

}

- (void)enterToDetailOfBookViewController :(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForJiaoYuPeiXun;
    bookVC.title = @"教育培训";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController1:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForMingXiaoGongKaiKe;
    bookVC.title = @"名校公开课";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController2:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForBaiJiaJiangTan;
    bookVC.title = @"百家讲坛";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController3:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForGuangBoJu;
    bookVC.title = @"广播剧";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController4:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForXiQu;
    bookVC.title = @"戏曲";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController5:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForDianTai;
    bookVC.title = @"电台";
    [vc.navigationController pushViewController:bookVC animated:NO];
}


@end
